package com.bw.module_video_detail.test_exoplayer


import android.net.Uri
import android.os.Bundle
import android.util.Log
import androidx.activity.enableEdgeToEdge
import androidx.annotation.OptIn
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.media3.common.MediaItem
import androidx.media3.common.PlaybackException
import androidx.media3.common.Player.Listener
import androidx.media3.common.util.UnstableApi
import androidx.media3.datasource.cache.CacheDataSource
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.exoplayer.analytics.AnalyticsListener
import androidx.media3.exoplayer.source.DefaultMediaSourceFactory
import androidx.media3.exoplayer.source.ProgressiveMediaSource
import androidx.media3.ui.PlayerView
import com.bw.module_video_detail.R

/**
 * 1.支持缓存
 *
 */
class ExoPlayerActivity : AppCompatActivity() {
    private lateinit var playerView:PlayerView
    private lateinit var exoPlayer:ExoPlayer
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_exo_player)
        playerView = findViewById(R.id.player_view)
        initPlayer()

    }

   private fun initPlayer() {


        //1。创建带缓存的工厂
        val cacheFactory = CacheDataSource.Factory()
            .setCache(VideoCacheManager.getCache(this))
            .setFlags(CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR) //缓存失败忽略错误

        val mediaSourceFactory = DefaultMediaSourceFactory(cacheFactory)

        exoPlayer = ExoPlayer.Builder(this)
            .setMediaSourceFactory(mediaSourceFactory) //设置缓存工厂
            .build()
        playerView.player = exoPlayer // 绑定播放器

        //设置数据源
        val mediaItem1 = MediaItem.fromUri(Uri.parse("http://txmov2.a.yximgs.com/upic/2020/08/23/20/BMjAyMDA4MjMyMDU3MzVfNDc1MTQ4NzZfMzQ4NzcwMzIxOTlfMV8z_b_B09a31110bdc2b61828d27ef27390ecec.mp4?tag=1-1599270586-vdg-1-h2fj3f9rtd-6b0b801241e3b41e&tt=b&bp=10000"))
      //  val mediaItem2 = MediaItem.fromUri(Uri.parse("http://txmov2.a.yximgs.com/upic/2020/08/23/20/BMjAyMDA4MjMyMDU3MzVfNDc1MTQ4NzZfMzQ4NzcwMzIxOTlfMV8z_b_B09a31110bdc2b61828d27ef27390ecec.mp4?tag=1-1599270586-vdg-1-h2fj3f9rtd-6b0b801241e3b41e&tt=b&bp=http://123.129.252.19/txmov2.a.yximgs.com/upic/2020/08/26/18/BMjAyMDA4MjYxODAyMzZfMTQ4NjAyMDU0OF8zNTA1NTQ0NjE3M18xXzM=_b_B669b122df328d8b6c389caee59e513ab.mp4?mkey=lego_ztc&tag=1-1599270586-vdg-1-efkwzf39mv-17d5eb2a2adc221e&mark_h=20&f=00&tt=b&bp=10000&sche_type=7"))
        val mediaSource = ProgressiveMediaSource.Factory(cacheFactory).createMediaSource(mediaItem1)
        //异步准备
        exoPlayer.setMediaSource(mediaSource)
        exoPlayer.prepare()
        //自动播放
        exoPlayer.playWhenReady = true
        //播放监听
        exoPlayer.addListener(object :Listener{
            override fun onPlayerError(error: PlaybackException) {
                super.onPlayerError(error)
                Log.d("TAG", "onPlayerError: "+error.message)
            }
            override fun onPlaybackStateChanged(playbackState: Int) {
                super.onPlaybackStateChanged(playbackState)
                when(playbackState){
                    ExoPlayer.STATE_READY -> {
                        //准备完成
                        Log.d("TAG", "准备完成: ")
                    }
                    ExoPlayer.STATE_ENDED -> {
                        //播放结束
                        Log.d("TAG", "播放结束: ")
                    }
                    ExoPlayer.STATE_BUFFERING -> {
                        //正在缓冲
                        Log.d("TAG", "正在缓冲: ")
                    }
                    ExoPlayer.STATE_IDLE -> {
                        //空闲
                        Log.d("TAG", "空闲: ")
                    }
                    else -> {
                        //其他
                    }
                }
            }

            override fun onMediaItemTransition(mediaItem: MediaItem?, reason: Int) {
                super.onMediaItemTransition(mediaItem, reason)
                Log.d("TAG", "切换歌曲: "+mediaItem+reason)
            }

        })

        //监听糊掉
        exoPlayer.addAnalyticsListener(object :AnalyticsListener{
            override fun onRenderedFirstFrame(
                eventTime: AnalyticsListener.EventTime,
                output: Any,
                renderTimeMs: Long
            ) {
                super.onRenderedFirstFrame(eventTime, output, renderTimeMs)
                Log.d("TAG", "首帧加载完毕: ")
            }
        })

    }
}